% Answer to the question no.11.2b of Lab Manual
% Simulating 1000 repetitions of the sequence of 1000 coin tosses
function LM11_2b(max)                                     % Using max to represent the number of equal bins in the histogram
r = zeros(1000,1000);                                     % Each column represents one repetitions of 1000 coin tosses
Com = 0.6*ones(1000,1);
Seq = zeros(1,1000);                                      % Each value represents the number of heads in this repetition
for i=1:1:1000                                            % Carrying out 1000 repetitions
    r(:,i) = rand(1000,1);
    Seq(i) = sum(r(:,i) < Com);
end
figure;
hist(Seq,max);xlim([500 700]);                            % plot the histogram of the number of heads obtained in each repetition
xlabel('The number of heads');ylabel('The probability');

if max<20                                                 % showing the broken line graph
    [N,X]=hist(Seq,max);
    hold on;
    plot(X,N,'-r');
else
end

end

